package com.dong.loadTimeWeaver;

import org.springframework.context.weaving.LoadTimeWeaverAware;
import org.springframework.instrument.classloading.LoadTimeWeaver;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

/**
 * description
 *
 * @author Administrator 2021/09/06 18:41
 */
public class MyBean implements LoadTimeWeaverAware {

    private LoadTimeWeaver loadTimeWeaver;

    @Override
    public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
        this.loadTimeWeaver = loadTimeWeaver;
    }

    public void loadTimeWeaverTest(){
        System.out.println(loadTimeWeaver.getInstrumentableClassLoader());
        System.out.println(loadTimeWeaver.getThrowawayClassLoader());
        loadTimeWeaver.addTransformer(new MyClassFileTransformer());
    }

    private static class MyClassFileTransformer implements ClassFileTransformer{

        @Override
        public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            return new byte[0];
        }
    }
}
